import zipfile
from itertools import product
from string import ascii_lowercase
from concurrent.futures import ThreadPoolExecutor
import cupy as cp

def attempt_password(zf, password):
    try:
        zf.extractall(pwd=password.encode('utf-8'))
        print(f'密码破解成功: {password}')
        return password
    except (RuntimeError, zipfile.BadZipFile):
        return None

def crack_word_password(file_path, max_length):
    with zipfile.ZipFile(file_path) as zf:
        with ThreadPoolExecutor() as executor:
            for length in range(1, max_length + 1):
                attempts = [''.join(attempt) for attempt in product(ascii_lowercase, repeat=length)]
                attempts_gpu = cp.array(attempts)
                for password in attempts_gpu:
                    future = executor.submit(attempt_password, zf, password)
                    result = future.result()
                    if result:
                        return result
    print('未能破解密码')
    return None

# 示例用法
# crack_word_password('path_to_protected_word_file.docx', 4)
